﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Script.Serialization;
using RBS.Common.JsonEntity;

namespace RBS.Web.Models
{
    public class ApiRepository
    {
        public static  JsonData Get(string apiUrl)
        {
            var jsonData = new JsonData();
            var client = new HttpClient { BaseAddress = new System.Uri(RBS.Common.ConfigHelper.GetConfigString("webApi")) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("appliction/json"));
            HttpResponseMessage response = client.GetAsync(apiUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<JsonData>(response.Content.ReadAsStringAsync().Result);
                return model;
            }
            else
            {
                jsonData.Success = false;
                jsonData.Error = "调用错误";
            }
            return jsonData;
        }

        public static JsonData Post(string apiUrl, HttpContent content)
        {
            var jsonData = new JsonData();
            var client = new HttpClient { BaseAddress = new System.Uri(RBS.Common.ConfigHelper.GetConfigString("webApi")) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
            HttpResponseMessage response = client.PostAsync(apiUrl, content).Result;
            if (response.IsSuccessStatusCode)
            {
                   var serializer = new JavaScriptSerializer(); 
                   var model = serializer.Deserialize<JsonData>(response.Content.ReadAsStringAsync().Result);
                return model;
                /*   jsonData.Data = response.Content.ReadAsStringAsync().Result;
                jsonData.Success = true;*/
            } 
            return jsonData;
        }

        public static JsonData Put(string apiUrl, HttpContent content)
        {
            var jsonData = new JsonData();
            var client = new HttpClient { BaseAddress = new System.Uri(RBS.Common.ConfigHelper.GetConfigString("webApi")) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("appliction/json"));
            HttpResponseMessage response = client.PutAsync(apiUrl, content).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<JsonData>(response.Content.ReadAsStringAsync().Result);
                return model;
            }
            else
            {
                jsonData.Success = false;
                jsonData.Error = "调用错误";
            }
            return jsonData;
        }
        public static JsonData Delete(string apiUrl)
        {
            var jsonData = new JsonData();
            var client = new HttpClient { BaseAddress = new System.Uri(RBS.Common.ConfigHelper.GetConfigString("webApi")) };
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("appliction/json"));
            HttpResponseMessage response = client.DeleteAsync(apiUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                var serializer = new JavaScriptSerializer();
                var model = serializer.Deserialize<JsonData>(response.Content.ReadAsStringAsync().Result);
                return model; 
            }
            else
            {
                jsonData.Success = false;
                jsonData.Error = "调用错误";
            }
            return jsonData;
        }
    }
}